 
  

 






<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do?Id=210 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:28:08 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
<head>
 <title>
  Java Practices -> Beware of DecimalFormat
 </title>
 <link rel="stylesheet" type="text/css" href="../stylesheet8.css" media="all">
 
 <link rel="shortcut icon" href='../images/favicon.ico' type="image/vnd.microsoft.icon">
 <meta name="description" content="Concise presentations of java programming practices, tasks, and conventions, amply illustrated with syntax highlighted code examples.">
 
 <meta name='keywords' content='java,java programming,java practices,java idiom,java style,java design patterns,java coding conventions,'>
 
 
</head>
 
<body>


<div class='menu-bar'>
 
  <a href='../home/HomeAction.html' title='Table of Contents'>Home</a> |
  <a href='../vote/VoteSummaryAction-2.html' title='View Poll Results'>Poll</a> |
   
  <A href='../feedback/FeedbackAction451f-2.html?Operation=Show' title='Send Your Feedback'>Wiki</a> |
  <b><a href='../source/SourceAction-2.html' title='Grab Source Code'>Source Code</a></b><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |

  <a href='http://www.web4j.com/Java_Web_Application_Framework_Overview.jsp?From=1' title='Free Download - Java Web Application Framework'><b>WEB4J</b></a> |
  
  <a href='http://www.date4j.net/' title='Replacement for java.util.Date'><b>DATE4J</b></a> |

   <a href='../references/ReferencesAction-2.html' title='References'>Links</a>
   
  <form action='http://www.javapractices.com/search/SearchAction.do' method='get' class='search-form'>
   <input type='text' name='SearchTerms' value="" size=12 maxlength=50 class='search'>
   <input type='submit' value="Search">
  </form>
 
</div>

<P>



  

 






<p class="display-messages">

 

 

</p>


<div class="main-layout">
 
   

 




<div class='page-title'>Beware of DecimalFormat</div>

<div class='main-body'>
 
<br>Parsing user input into numbers (especially numbers representing money) is a very common task. Such parsing is 
often implemented with <a href="http://java.sun.com/javase/6/docs/api/java/text/DecimalFormat.html">DecimalFormat</a>. 
However, parsing with <tt>DecimalFormat</tt> should be done with care. 
You may even want to consider an alternative. Here is why :
<ul>
<li>surprisingly, a <a href="http://java.sun.com/javase/6/docs/api/java/text/Format.html">Format</a> does not 
need to parse <em>all</em> of its input. That is, if it finds a 
match at the <em>start</em> of an input <tt>String</tt>, it will cease parsing, and return a value. When parsing user input, this 
kind of behavior is not usually desired. To accomodate this behavior, an extra step is needed - a 
<a href="http://java.sun.com/javase/6/docs/api/java/text/ParsePosition.html">ParsePosition</a> object 
needs to be passed as an <a href="TopicAction6a39-2.html">output parameter</a> to <tt>DecimalFormat</tt>. 
<li>the class seems to be rather <a href='http://bugs.sun.com/bugdatabase/'>buggy</a>.
<li>the class is harder than most to understand.
</ul>

<P>Here is an example of parsing with <tt>DecimalFormat</tt>. An example run appears below. As you can see, 
<ul>
<li>parsing user input will always require the caller to use 
<a href="http://java.sun.com/javase/6/docs/api/java/text/ParsePosition.html">ParsePosition</a> to detect if 
the whole input has been parsed.
<li>there are cases when the parsing seems just plain <em>wrong</em>.
</ul>
<br>
<PRE>

<span class='keyword'>import</span> java.text.*;

<span class='keyword'>public</span> <span class='keyword'>final</span> <span class='keyword'>class</span> BewareDecimalFormat {
  
  <span class='comment'>/**
  * parseUserInput DecimalFormat.
  */</span> 
  <span class='keyword'>public</span> <span class='keyword'>static</span> <span class='keyword'>void</span> main (String... aArguments) {

    String patternOne = <span class='literal'>"#,##0.00"</span>;
    log(<span class='literal'>"Format : "</span> + patternOne);
    parseUserInput(<span class='literal'>"1000"</span>, patternOne);
    parseUserInput(<span class='literal'>"1,000.00"</span>, patternOne);
    parseUserInput(<span class='literal'>"1000.33"</span>, patternOne);
    parseUserInput(<span class='literal'>".20"</span>, patternOne);
    parseUserInput(<span class='literal'>".2"</span>, patternOne); 
    parseUserInput(<span class='literal'>".222"</span>, patternOne); 
    parseUserInput(<span class='literal'>".222333444"</span>, patternOne); 
    parseUserInput(<span class='literal'>"100.222333444"</span>, patternOne); 
    parseUserInput(<span class='literal'>".22A"</span>, patternOne); 
    parseUserInput(<span class='literal'>".22BlahA"</span>, patternOne); 
    parseUserInput(<span class='literal'>"22Blah"</span>, patternOne); 
    parseUserInput(<span class='literal'>"Blah22"</span>, patternOne); 
    parseUserInput(<span class='literal'>"100000,000.00"</span>, patternOne);
    parseUserInput(<span class='literal'>"10,0000,000.00"</span>, patternOne);
    parseUserInput(<span class='literal'>"1,0000,0000.00"</span>, patternOne);

    String patternTwo= <span class='literal'>"#,###0.00"</span>; <span class='comment'>//group every 4 digits, not 3
</span>    log(<span class='literal'>" "</span>);
    log(<span class='literal'>"Format : "</span> + patternTwo);
    parseUserInput(<span class='literal'>"1000.00"</span>, patternTwo);
    parseUserInput(<span class='literal'>"1,000.00"</span>, patternTwo);
    parseUserInput(<span class='literal'>"1,0000.00"</span>, patternTwo);
    parseUserInput(<span class='literal'>"1,00000.00"</span>, patternTwo);
    log(<span class='literal'>"Done."</span>);
  }

  <span class='comment'>// PRIVATE //
</span>
  <span class='keyword'>private</span> <span class='keyword'>static</span> <span class='keyword'>void</span> parseUserInput(String aValue, String aDecimalFormatPattern) {
    DecimalFormat format = <span class='keyword'>new</span> DecimalFormat(aDecimalFormatPattern);
    ParsePosition parsePosition = <span class='keyword'>new</span> ParsePosition(<span class='literal'>0</span>);
    Object object = format.parse(aValue, parsePosition);

    <span class='keyword'>if</span>( object == <span class='keyword'>null</span> ) {
      log(<span class='literal'>"Failed to parse: "</span> + aValue);
    }
    <span class='keyword'>else</span> <span class='keyword'>if</span>( parsePosition.getIndex() &lt; aValue.length() ) {
      log(
        aValue + <span class='literal'>" parsed OK (not whole input) ParsePos:"</span> + 
        parsePosition.getIndex() + <span class='literal'>", Parse Result: "</span> + object 
      );
    }
    <span class='keyword'>else</span> {
      log(
        aValue + <span class='literal'>" parsed OK. ParsePos: "</span> + parsePosition.getIndex() + 
        <span class='literal'>", Parse Result: "</span> + object
      );
    } 
  }

  <span class='keyword'>private</span> <span class='keyword'>static</span> <span class='keyword'>void</span> log(Object aMessage){
    System.out.println(String.valueOf(aMessage));
  }
} 
</PRE>
<br>
<br>Example run :
<PRE>
>java -cp . BewareDecimalFormat
Format : #,##0.00
1000 parsed OK. ParsePos: 4, Parse Result: 1000
1,000.00 parsed OK. ParsePos: 8, Parse Result: 1000
1000.33 parsed OK. ParsePos: 7, Parse Result: 1000.33
.20 parsed OK. ParsePos: 3, Parse Result: 0.2
.2 parsed OK. ParsePos: 2, Parse Result: 0.2
.222 parsed OK. ParsePos: 4, Parse Result: 0.222
.222333444 parsed OK. ParsePos: 10, Parse Result: 0.222333444
100.222333444 parsed OK. ParsePos: 13, Parse Result: 100.222333444
.22A parsed OK (not whole input) ParsePos:3, Parse Result: 0.22
.22BlahA parsed OK (not whole input) ParsePos:3, Parse Result: 0.22
22Blah parsed OK (not whole input) ParsePos:2, Parse Result: 22
Failed to parse: Blah22
100000,000.00 parsed OK. ParsePos: 13, Parse Result: 100000000
10,0000,000.00 parsed OK. ParsePos: 14, Parse Result: 100000000
1,0000,0000.00 parsed OK. ParsePos: 14, Parse Result: 100000000

Format : #,###0.00
1000.00 parsed OK. ParsePos: 7, Parse Result: 1000
1,000.00 parsed OK. ParsePos: 8, Parse Result: 1000
1,0000.00 parsed OK. ParsePos: 9, Parse Result: 10000
1,00000.00 parsed OK. ParsePos: 10, Parse Result: 100000
Done.</PRE>
<br>
<br>

</div>





<div class='topic-section'>Would you use this technique?</div>
<div class='main-body'>
  
  <form action="http://www.javapractices.com/vote/AddVoteAction.do" method='post'>
    Yes<input type='radio' name='Choice' value='Y' >
    &nbsp;&nbsp;No<input type='radio' name='Choice' value='N'>
    &nbsp;&nbsp;Undecided<input type='radio' name='Choice' value="?" >
    &nbsp;&nbsp;<input type=submit value="Vote" >
    <input type='hidden' name='Operation' value='Apply'>
    <input type='hidden' name='TopicId' value='210'>
  </form>
</div>

<div style='height:10.0em;'></div>

 
 
</div>

  

 





<div align='center' class='legalese'>  
&copy; 2011 Hirondelle Systems |
<a href='../source/SourceAction-2.html'><b>Source Code</b></a><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |
<a href="mailto:webmaster@javapractices.com">Contact</a> |
<a href="http://creativecommons.org/licenses/by-nc-sa/1.0/">License</a> |
<a href='../apps/cjp.rss'>RSS</a>
<!-- ukey="2AC36CD2" -->
<!-- ckey="16DF3D87" -->
<br>

 Individual code snippets can be used under this <a href='../LICENSE.txt'>BSD license</a> - Last updated on June 6, 2010.<br>
 Over 150,000 unique IPs last month - <span title='Java Practices 2.6.5, Mon May 16 00:00:00 EDT 2011'>Built with</span> <a href='http://www.web4j.com/'>WEB4J</a>.<br>
 - In Memoriam : Bill Dirani -
</div>

<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2633428-1";
urchinTracker();
</script>



</body>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do?Id=210 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:28:08 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
</html>
